<?php 
    class SanPhamController extends BaseController{
        
        protected $sanphamTable = 'sanpham';
        protected $categoryTable = 'category';
        protected $subcategoryTable = 'subcategory';
        //Frontend
        public function ds(){
            $sanpham = DB::table('sanpham')
                ->paginate(10);
            return View::make('frontend.index')->with(array('sanpham'=>$sanpham));
        }

        public function productDetails($id)
        {
            $sanpham = DB::table('sanpham')->find($id); //where('sanpham.id','=', $id)->get();        
            if(is_null($sanpham))
                return View::make('frontend.error');                
            return View::make('frontend.details')->with(array('details' => $sanpham));
        }
        public function ViewCheckOut()
        {
            return View::make('frontend.checkout');
        }
        public function CheckOut()
        {
            Mail::send('emails.auth.reminder', array('key' => 'value'), function($message)
            {
                $message->to('tintran2008@gmail.com', 'John Smith')->subject('Welcome!');
            });
            return View::make('frontend.Succes');
        }
        //Backend
        public function show(){
               $filter_title = Input::get('title');
        		if($filter_title != '') {
    				$articles = DB::table('sanpham')
				    ->select('sanpham.id','sanpham.tensanpham','sanpham.motasanpham','sanpham.chitietsanpham','sanpham.hinhanh','sanpham.ngaytao','sanpham.dongia')					
                    ->whereRaw('sanpham.tenssanpham like ?',array('%'.$filter_title.'%'))					
                    ->paginate(10);
        		} else {    				
    				$articles = DB::table('sanpham')
   				     ->select('sanpham.id','sanpham.tensanpham','sanpham.motasanpham','sanpham.chitietsanpham','sanpham.hinhanh','sanpham.ngaytao','sanpham.dongia')                   
                    ->paginate(10);                
        		}
                return View::make('backend.sanpham.ds_sanpham')->with(array('article' => $articles));
        }
        public function add(){
                return View::make('backend.sanpham.add_sanpham');
        }
        public function store()
    	{
        		$title = Input::get('tensanpham');
                $dongia = Input::get('dongia');
        		$txtMota = Input::get('mota'); 
                $txtChitiet = Input::get('chitiet');     		
        		$create  = Input::get('create');
                if(Input::hasFile('image-file3')){
    				$destinationPath = 'uploads/sanpham/';
    				$image3  = str_random(6).'_'.Input::file('image-file3')->getClientOriginalName();
    				$uploadsucess = Input::file('image-file3')->move($destinationPath,$image3);
    			}
        		else{
        
        				$image3 = '';
        		}
                $ck_lang =Input::get('ck_lang');    
    //    		// insert data in datase
        		$article = DB::table('sanpham')->insertGetId(
            		array('tensanpham' => $title,'motasanpham' => $txtMota,'chitietsanpham' => $txtChitiet,'ngaytao'=>$create,'hinhanh'=>$image3,'dongia'=>$dongia ));
        		return Redirect::to('admin/sanpham/');
    	}
        public function destroy($id)
    	{	
        		$article = DB::table('sanpham')->where('id', '=', $id)->delete();        
        		return Redirect::to('admin/sanpham');
    	}
        
        public function edit($id)
    	{
    	    $article = DB::table('sanpham')
                        ->select('sanpham.id','sanpham.tensanpham','sanpham.motasanpham','sanpham.chitietsanpham','sanpham.hinhanh','sanpham.ngaytao','sanpham.dongia')
						->where('sanpham.id', '=', $id)
                        ->first();
    		return View::make('backend.sanpham.edit_sanpham')->with(array('article' => $article));
    	}
        public function update($id)
    	{    		
        		$title = Input::get('tensanpham');
                $dongia = Input::get('dongia');
        		$txtMota = Input::get('mota'); 
                $txtChitiet = Input::get('chitiet');     		
        		$create  = Input::get('create');
                if(Input::hasFile('new-image3')){
    				$destinationPath = 'uploads/sanpham/';
    				$image3  = str_random(6).'_'.Input::file('new-image3')->getClientOriginalName();
    				$uploadsucess = Input::file('new-image3')->move($destinationPath,$image3);
    			}
        		else{
        
        				$image3 = Input::get('old-image3');
        		}
    			DB::table('sanpham')
                ->where('id', $id)
                ->update(array('tensanpham' => $title,'motasanpham' => $txtMota,'chitietsanpham' => $txtChitiet,'ngaytao'=>$create,'hinhanh'=>$image3,'dongia'=>$dongia ));
        		return Redirect::to('admin/sanpham');
    	}
        
        
}
?>